package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.query.RechargeOfflineQuery;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.business.service.IUserBankInfoService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RechargeOfflineController {
        @Autowired
        private IUserBankInfoService userBankInfoService;
        @Autowired
        private IRechargeOfflineService rechargeOfflineService;
        @Autowired
        private IUserInfoService userInfoService;

        @RequestMapping("rechargeOffline")
        @IsLoginAnno
        public String rechargeOffline(@ModelAttribute("qo")RechargeOfflineQuery qo, Model model){
                //查出平台的银行卡,模拟已经找到了平台的财务ID
                LoginInfo loginInfo = UserContext.getLoginInfo();
                List<UserBankInfo> userBankInfos = userBankInfoService.listMybank(loginInfo.getId());
                PageResult result = rechargeOfflineService.query(qo);
                model.addAttribute("banks",userBankInfos);
                model.addAttribute("pageResult",result);
                return "/rechargeoffline/list";
        }

        /**
         * 审核用户充值的方法
         * @param id
         * @param remark
         * @param state
         * @return
         */
        @RequestMapping("rechargeOffline_audit")
        @ResponseBody
        public JSONResult rechargeOfflineAudit(Long id,String remark,Integer state){
                JSONResult jsonResult = new JSONResult();
                try {
                        rechargeOfflineService.rechargeOfflineAudit(id,remark,state);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;

        }

}
